# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

"""create mindrecord for training NanoDet."""
import os
import moxing as mox
from src.dataset import create_mindrecord
from src.model_utils.config import config

def EnvToObs(train_dir, obs_train_url):
    try:
        mox.file.copy_parallel(train_dir, obs_train_url)
        print("Successfully Upload {} to {}".format(train_dir,obs_train_url))
    except Exception as e:
        print('moxing upload {} to {} failed: '.format(train_dir,obs_train_url) + str(e))
    return 

if __name__ == "__main__":

    local_data_url = config.data_url
    print(os.path.exists(local_data_url))
    config.data_url = '/home/work/user-job-dir/data/'
    try:    
        mox.file.copy_parallel(local_data_url, config.data_url)
        print("Successfully Download {} to {}".format(local_data_url,config.data_url))
    except Exception as e:
        print('moxing download {} to {} failed: '.format(local_data_url, config.data_url) + str(e))

    dir1 = "/cache/user-job-dir"
    dir2 = "/home/work/user-job-dir"
    str1 = sorted(os.walk(dir1))
    str2 = sorted(os.walk(dir2))
    for root, dirs, files in sorted(str1):
        print(root)
        print(dirs)
        print(files)
    for root, dirs, files in sorted(str2):
        print(root)
        print(dirs)
        print(files)

    mindrecord_file = create_mindrecord(config.create_dataset, config.prefix, config.is_training)

    train_dir = '/cache/output'
    EnvToObs(train_dir, config.train_url)
